# -*- coding: utf-8 -*-
"""
---------------------------------------
@File    ：ip_server_add.py
@Author  ：liyunhui
@Date    ：2024/9/13 下午3:38 
@explain : 拨号云服务器添加
---------------------------------------
"""
import redis
import openpyxl
from config import config


# 读取Excel文件
file_path = "拨号机.xlsx"
workbook = openpyxl.load_workbook(file_path)
sheet = workbook.active

# 存储结果
results = []

# 遍历每一行（跳过表头）
for row in sheet.iter_rows(min_row=2, values_only=True):
    username, product_name, server_name, password, ip_port, adsl_user, intranet_ip, _, _ = row

    # 分割IP和端口
    ip, port = ip_port.split(":")

    # 判断是否为国内
    is_domestic = intranet_ip in config.ip_classify["is_china"]

    # 构建JSON对象
    entry = {
        "ip": ip,
        "port": int(port),
        "user": "root",
        "password": password,
        "is_domestic": is_domestic,
        "server_name": server_name,
        "intranet_ip": intranet_ip,
    }
    results.append(entry)

key = config.ip_pool_total_ip
r = redis.Redis(host=config.redis_host, port=config.redis_port, db=config.redis_db, password=config.redis_password)
for ip in results:
    intranet_ip = ip.get('intranet_ip')
    redis_key = f"{key}:{intranet_ip}"
    ip['is_domestic'] = str(ip['is_domestic'])
    r.hmset(redis_key, ip)



















